import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import( /* webpackChunkName: "login_home_welcome" */ '../views/Login.vue')
// import Login from '../views/Login.vue'
// import Home from '../components/home.vue'
const Home = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/home.vue')
// import Welcome from '../components/welcome.vue'
const Welcome = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/welcome.vue')
// import User from '../components/users/user.vue'
const User = () => import( /* webpackChunkName: "user_jurisdiction_roles" */ '../components/users/user.vue')
// import Jurisdiction from '../components/power/jurisdiction.vue'
const Jurisdiction = () => import( /* webpackChunkName: "user_jurisdiction_roles" */ '../components/power/jurisdiction.vue')
// import Roles from '../components/power/role.vue'
const Roles = () => import( /* webpackChunkName: "user_jurisdiction_roles" */ '../components/power/role.vue')
// import Categories from '../components/goods/categories.vue'
const Categories = () => import( /* webpackChunkName: "categories_params_goodstwo" */ '../components/goods/categories.vue')
// import Params from '../components/goods/params.vue'
const Params = () => import( /* webpackChunkName: "categories_params_goodstwo" */ '../components/goods/params.vue')
// import Goodstwo from '../components/goods/goodstwo'
const Goodstwo = () => import( /* webpackChunkName: "categories_params_goodstwo" */ '../components/goods/goodstwo')
// import Addgoods from '../components/goods/addgoods.vue'
const Addgoods = () => import( /* webpackChunkName: "addgoods_orders_reports" */ '../components/goods/addgoods.vue')
// import Orders from '../components/orders/orders.vue'
const Orders = () => import( /* webpackChunkName: "addgoods_orders_reports" */ '../components/orders/orders.vue')
// import Reports from '../components/reports/reports'
const Reports = () => import( /* webpackChunkName: "addgoods_orders_reports" */ '../components/reports/reports')

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  component: Login
}, {
  path: '/home',
  component: Home,
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: Welcome
  }, {
    path: '/users',
    component: User
  }, {
    path: '/rights',
    component: Jurisdiction
  }, {
    path: '/roles',
    component: Roles
  }, {
    path: '/categories',
    component: Categories
  }, {
    path: '/params',
    component: Params
  }, {
    path: '/goods',
    component: Goodstwo
  }, {
    path: '/goods/add',
    component: Addgoods
  }, {
    path: '/orders',
    component: Orders
  }, {
    path: '/reports',
    component: Reports
  }]
}]
const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // form从哪个路径跳转而来
  // next是一个函数，代表放行
  if (to.path === '/login') return next()
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  // 对tokenStr进行判断，如果登入了就进入home页面，没就就强制跳转到登入页面
  if (!tokenStr) return next('/login')
  next()
})
export default router
